package ddd.ajava.chapter6.question2;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * 判断从键盘输入的一串整数哪些是素数
 * 1.从键盘读入数据存入List里
 * 2.找出最小数min和最大数max
 * 3.判断从min到max的平方的素数为基础
 * 4.遍历List中的数，与3中的素数相与
 * 5.键盘控制查询结果，1输入数据  2查询素数 3退出
 */
public class JudgePrime {
	//输入数据
	public List<Integer> inputData(){
		Scanner scanner=null;
		List<Integer> inputList=null;
		System.out.println("请输入要判定的数据：");
		try{
			scanner=new Scanner(System.in);
			String inputdata=scanner.nextLine();
			System.out.println("inputData:"+inputdata);
			String[] inputdatas=inputdata.split(" +");
			for(String s:inputdatas){
				System.out.print("'"+s+"' ");
			}
			System.out.println();
			inputList=new ArrayList<Integer>();
			for(String d:inputdatas){
				int value=Integer.parseInt(d);
				inputList.add(value);
			}
		}catch(NumberFormatException e){
			System.out.println("输入数据类型错误，请重新输入整数数字！");
			inputData();
		}
		return inputList;
	}
	
	public List<Integer> getTempPrime(List<Integer> inputList){
		int max=Integer.MIN_VALUE;
		for(Integer i:inputList){
			if(i>max) max=i;
		}
		
		List<Integer> tempPrime=new ArrayList<Integer>();
		for (int i = 2; i <= Math.sqrt(max); i++) {
			if (isPrime(i)) {
				tempPrime.add(i);
				System.out.print("tempPrime:"+i + " ");
			}
		}
		System.out.println();
		return tempPrime;
	}
	
	//直接判断是否是素数
	private boolean isPrime(int prime){
		if(prime==1)
			return false;
		for(int i=2;i<=Math.sqrt(prime);i++){
			if(prime%i==0)
				return false;
		}
		return true;
	}
	
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int control=0;
		JudgePrime jp=new JudgePrime();
		List<Integer> inputdatas=null;
		List<Integer> tempPrime=null;
		
		ExecutorService executorService=Executors.newFixedThreadPool(10);
		ConcurrentHashMap<Integer, Boolean> primeNumberFutures=null;
		
		while(true){
			System.out.println("请输入命令:1输入数据  2查询素数 3退出");
			control=sc.nextInt();
			switch(control){
			case 1:{
				primeNumberFutures=new ConcurrentHashMap<Integer, Boolean>();
				inputdatas=jp.inputData();
				tempPrime=jp.getTempPrime(inputdatas);
				
				for(int data:inputdatas){
					PrimeThread pThread=new PrimeThread(data, tempPrime);
					Future<Boolean> re=executorService.submit(pThread);
					try {
						primeNumberFutures.put(data,re.get());
					} catch (InterruptedException e) {
						e.printStackTrace();
					} catch (ExecutionException e) {
						e.printStackTrace();
					}
				}
				break;
			}
			case 2:
				Set<Integer> keys=primeNumberFutures.keySet();
				System.out.print("prime data:");
				for(int key:keys){
					if(primeNumberFutures.get(key)){
						System.out.print(key+" ");
					}
				}
				System.out.println();
				break;
			case 3:
				System.exit(0);
				break;
			}
		}
	}
}
